using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FoodCon : MonoBehaviour
{
    public BoxCollider2D _gridAera;
    public LayerMask _snakeBodyLayer;
    Vector2 _spawnPosition;
    Collider2D _hitCollider;
    Collider2D _hitCollider2;
    float x;
    float y;
    public GameObject _speedFood;
    public GameObject _speedCutFood;
    public LayerMask _ObstacleLayer;
    private void Start()
    {
        RomPostion();
    }
    //随机生成食物位置
    private void RomPostion() 
    {
        Bounds bounds = _gridAera.bounds;
        do
        {
            x = Random.Range(bounds.min.x, bounds.max.x);
            y = Random.Range(bounds.min.y, bounds.max.y);
            _spawnPosition = new Vector2(x, y);
            //检测生成位置是否是蛇身体位置
            _hitCollider = Physics2D.OverlapCircle(_spawnPosition, 0.1f, _snakeBodyLayer);
            _hitCollider2 = Physics2D.OverlapCircle(_spawnPosition, 0.1f, _ObstacleLayer);
        }
        while (_hitCollider != null || _hitCollider2 != null);
        this.transform.position = new Vector3(Mathf.Round(x), Mathf.Round(y), 0);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            RomPostion();
            int random = Random.Range(0, 10);
            if (random == 5)
            {
                _speedFood.SetActive(true);
                _speedFood.GetComponent<SpeedFoodCon>().CreateSpeedUpFood();
            }
            if (random == 6 || random == 4)
            {
                _speedCutFood.SetActive(true);
                _speedCutFood.GetComponent<SpeedCutFood>().CreateSpeedCutFood();
            }
        }
        else
        {
            RomPostion();
        }
    }
}
